package com.zxc.ompz;

import java.io.IOException;

import javax.microedition.io.Connector;
import javax.microedition.io.file.FileConnection;

import net.rim.device.api.system.Bitmap;
import net.rim.device.api.ui.Manager;
import net.rim.device.api.ui.Screen;
import net.rim.device.api.ui.Ui;
import net.rim.device.api.ui.UiApplication;
import net.rim.device.api.ui.UiEngine;
import net.rim.device.api.ui.component.Dialog;
/**
 * This class extends the UiApplication class, providing a
 * graphical user interface.
 */
public class OmpzApp extends UiApplication{
    /**
     * Entry point for application
     * @param args Command line arguments (not used)
     */
	static private OmpzApp theApp;
	static private OmpzScreen theScreen;
	static private LogScreen logScreen;
    public static void main(String[] args){
        // Create a new instance of the application and make the currently
        // running thread the application's event dispatch thread.
        theApp = new OmpzApp();
        theApp.checkWorkingSpace();
        theApp.loadScreen();
        theApp.enterEventDispatcher();
    }
    /**
     * Creates a new OmpzApp object
     */
    public OmpzApp() {}
    private void loadScreen(){
    	// Push a screen onto the UI stack for rendering.
    	theScreen = new OmpzScreen();
    	logScreen = new LogScreen();
        pushScreen(theScreen);
    }
	static public OmpzApp getTheApp(){
		return theApp;
	}
	static public OmpzScreen getTheScreen()	{
		return theScreen;
	}
	static public LogScreen getLogScreen(){
		return logScreen;
	}
	private boolean checkWorkingSpace(){
        String msg=null;
		FileConnection f = null;
		boolean succ = true;
		try {
			f = (FileConnection) Connector.open("file:///SDCard/Ompz/");
			try {
				if (!f.exists()){
					f.mkdir();
				}
			}
			catch(IOException e){
				msg = "Fail to make directory on SDCard!";
				succ = false;
			}
			finally{
				f.close();
			}
		}
		catch (IOException e){
			msg = "Fail to open /SDCard/Ompz/!";
			succ = false;
		}
        if (!succ){
        	Screen screen = new Dialog(Dialog.OK, msg,
        	        Dialog.OK, Bitmap.getPredefinedBitmap(Bitmap.EXCLAMATION), 
        	        Manager.VERTICAL_SCROLL);
        	Ui.getUiEngine().pushGlobalScreen(screen, 1, UiEngine.GLOBAL_QUEUE);
        }
        return succ;
	}
}

